
#ifdef _WINDOWS
#include <windows.h>
#endif

// include ACTL headers first, before alignment change
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include "Registry.h"
//--------------------- WINDOWS REGISTRY -------------------------------------------

// def. ACTL registry entry
#define SBXSTRING "Software\\Streambox RMS\\Settings\\"	


// to keep all registry names in one file, use REGISTRY_GetName

char* REGISTRY_GetName(int iKEYNAME)
{
	if (iKEYNAME==KEY_SERVER_IP)				return "Server IP";
	if (iKEYNAME==KEY_SERVER_PORT)				return "Server PORT";
	if (iKEYNAME==KEY_REFLECTOR_IP)				return "Reflector IP";
	if (iKEYNAME==KEY_ENCODER_IP)				return "Encoder IP";
	if (iKEYNAME==KEY_ENCODER_PORT)				return "Encoder Port";
	if (iKEYNAME==KEY_ENCODER_BITRATE)			return "Encoder Bit Rate";
	if (iKEYNAME==KEY_ENCODER_CYCLE)			return "Encoder Cycle";
	if (iKEYNAME==KEY_ENCODER_FRAMERATE)		return "Encoder Frame Rate";
	if (iKEYNAME==KEY_CLIENT_GDI)				return "Client GDI";
	if (iKEYNAME==KEY_CLIENT_GDIFULLSCREEN)		return "Client GDI Full Screen";
	if (iKEYNAME==KEY_REGISTRATION_NAME)		return "User name";
	if (iKEYNAME==KEY_REGISTRATION_KEY)			return "Registration key";
	if (iKEYNAME==KEY_ADDR)						return "Current Address";
	if (iKEYNAME==KEY_RING)						return "RING";
	if (iKEYNAME==KEY_CAST_CONF)				return "Broadcast/Conference";
	if (iKEYNAME==KEY_LAST_SITE_CONNECTED)		return "Last Connected Site";
	if (iKEYNAME==KEY_HOME_PATH)				return "HomePath";
    if (iKEYNAME==KEY_FEC_ENABLED)				return "EnableFEC";
    if (iKEYNAME==KEY_FEC_PARAM1)				return "FECparam1";
    if (iKEYNAME==KEY_FEC_PARAM2)				return "FECparam2";
#ifdef _J707_
	//
	// #J7070022
	if (iKEYNAME==KEY_VIDEO)					return "Video";
    if (iKEYNAME==KEY_AUDIO)					return "Audio";
#endif

	MessageBox(0,"Key name not found","Fatal error",MB_OK);
	exit(0);
	return 0;
}

// please USE 4-letter key
int REGISTRY_LoadInt(int iKEYNAME,int default_value)
{
	LONG res;
	HKEY hKey;
	DWORD lLen;
	DWORD dwDisp;
	DWORD value;
	DWORD dwType;

	char *KEYNAME=REGISTRY_GetName(iKEYNAME);

	value=default_value;
	res = RegOpenKeyEx(HKEY_CURRENT_USER,SBXSTRING,0,KEY_ALL_ACCESS,&hKey);
	if(ERROR_SUCCESS != res) {
		res = RegCreateKeyEx(HKEY_CURRENT_USER,SBXSTRING,0,NULL,
			REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisp);
		if(ERROR_SUCCESS != res) return value;
	}
	lLen = sizeof(DWORD);
	res = RegQueryValueEx(hKey,KEYNAME,0,&dwType,(BYTE*)&(value),&lLen);
	RegCloseKey(hKey);
	return value;
}
int REGISTRY_SaveInt(int iKEYNAME,int value)
{
	LONG res;
	HKEY hKey;
	DWORD dwDisp;
	char *KEYNAME=REGISTRY_GetName(iKEYNAME);
	

	res = RegOpenKeyEx(HKEY_CURRENT_USER,SBXSTRING,0,KEY_ALL_ACCESS,&hKey);
	if(ERROR_SUCCESS != res)
	{
		res = RegCreateKeyEx(HKEY_CURRENT_USER,SBXSTRING,0,NULL,
			REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisp);
		if(ERROR_SUCCESS != res) return 0;
	}
	res = RegSetValueEx(hKey,KEYNAME,0,REG_DWORD,(BYTE*)&value,sizeof(DWORD));
	RegCloseKey(hKey);
	return (ERROR_SUCCESS == res);
}

int REGISTRY_LoadString(int iKEYNAME,char* szKey, int nLen)
{
	if((!szKey) || (nLen <=0)) return 0;

	LONG res;
	HKEY hKey;
	DWORD lLen;
	DWORD dwDisp;
	DWORD dwType;

	char *KEYNAME=REGISTRY_GetName(iKEYNAME);

	szKey[0] = 0;
	res = RegOpenKeyEx(HKEY_CURRENT_USER,SBXSTRING,0,KEY_ALL_ACCESS,&hKey);
	if(ERROR_SUCCESS != res) {
		res = RegCreateKeyEx(HKEY_CURRENT_USER,SBXSTRING,0,NULL,
			REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisp);
		if(ERROR_SUCCESS != res) return 0;
	}
	
	lLen = nLen;
	res = RegQueryValueEx(hKey,KEYNAME,0,&dwType,(BYTE*)szKey,&lLen);
	RegCloseKey(hKey);
	if(ERROR_SUCCESS != res) return 0;
	return 1;
}
int REGISTRY_SaveString(int iKEYNAME,const char* szKey)
{
	if(!szKey) return 0;

	LONG res;
	HKEY hKey;
	DWORD dwDisp;
	char *KEYNAME=REGISTRY_GetName(iKEYNAME);
	

	res = RegOpenKeyEx(HKEY_CURRENT_USER,SBXSTRING,0,KEY_ALL_ACCESS,&hKey);
	if(ERROR_SUCCESS != res)
	{
		res = RegCreateKeyEx(HKEY_CURRENT_USER,SBXSTRING,0,NULL,
			REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisp);
		if(ERROR_SUCCESS != res) return 0;
	}
	res = RegSetValueEx(hKey,KEYNAME,0,REG_SZ,(BYTE*)szKey,strlen(szKey)+1);
	RegCloseKey(hKey);
	return (ERROR_SUCCESS == res);
}
